package com.thread.sync;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Synchronized1 {

	public static void main(String[] args) {
		ExecutorService exec = Executors.newFixedThreadPool(3);
		final Synchronized1 gCode = new Synchronized1();
		for (int i = 0; i < 5; i++) {
			exec.execute(new Runnable() {
				@Override
				public void run() {
					// 写在这里是错误的，因为synchronized无法锁住其它对象
					// Synchronized1 gCode = new Synchronized1();
					gCode.syncCurrentObject();
				}
			});
		}
		exec.shutdown();
	}

	public synchronized void syncCurrentObject() {
		System.out.println(Thread.currentThread().getName() + "..start.." + "-----" + System.currentTimeMillis());
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName() + "..end.." + "-----" + System.currentTimeMillis());

	}
}
